using System;
using System.Windows.Forms;

namespace AreaBuilder.Forms {
    public partial class frmHelp : Form {
        private readonly AreaHolder ah;
        private int inArray = -1;

        public frmHelp() {
            InitializeComponent();
        }

        public frmHelp(AreaHolder ah, int value) {
            InitializeComponent();

            this.ah = ah;
            inArray = value;
        }

        private void frmHelp_Load(object sender, EventArgs e) {
            if (inArray >= 0) {
                inArray--;

                if (((Help) ah.area.Helps[inArray]).MinLevel <= 1) {
                    chkNoMin.Checked = true;
                    txtMinLevel.Enabled = false;
                }
                txtMinLevel.Text = ((Help) ah.area.Helps[inArray]).MinLevel.ToString();
                txtKeywords.Text = ((Help) ah.area.Helps[inArray]).Keywords;
                txtDesc.Text = ((Help) ah.area.Helps[inArray]).Description.Replace("\n", Environment.NewLine);
            }
        }

        private void chkNoMin_CheckedChanged(object sender, EventArgs e) {
            txtMinLevel.Enabled = !chkNoMin.Checked;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            Dispose();
        }

        private void btnOK_Click(object sender, EventArgs e) {
            Help h;

            if (inArray >= 0)
                h = (Help) ah.area.Helps[inArray];
            else
                h = new Help();

            h.MinLevel = (chkNoMin.Checked ? 1 : Int32.Parse(txtMinLevel.Text));
            h.Keywords = txtKeywords.Text;
            h.Description = txtDesc.Text;

            if (inArray < 0)
                ah.area.Helps.Add(h);

            Dispose();
        }

        private void txtMinLevel_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == 13)
                btnOK_Click(null, null);

            if (!Char.IsDigit(e.KeyChar))
                return;
        }

        private void txtKeywords_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == 13)
                btnOK_Click(null, null);
        }
    }
}